package com.ynding.cloud.common.util.bean;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.core.internal.Function;

import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

/**
 * <p> </p>
 *
 * @author ynding
 * @version 2021/5/27
 **/
public class BeanUtil {

    /**
     * 分页转换, 支持一个参数
     *
     * @return
     */
    public static <D, T> Page<D> toDtoPage(Page<T> source,
                                           Function<T, D> convert) {
        if (source != null) {
            Page<D> dest = new Page<>(source.getCurrent(), source.getSize(), source.getTotal());
            if (CollectionUtil.isNotEmpty(source.getRecords())) {
                List<T> list = source.getRecords();
                List<D> ds = list.stream().map(m -> convert.apply(m)).collect(Collectors.toList());
                dest.setRecords(ds);
            }
            return dest;
        }
        return null;
    }


    /**
     * 分页转换, 支持两个参数
     *
     * @return
     */
    public static <D, T, V> Page<V> toDtoPage2(Page<T> source,
                                               BiFunction<T, D, V> convert, Class<D> dClass) {
        if (source != null) {
            Page<V> dest = new Page<>(source.getCurrent(), source.getSize(), source.getTotal());
            if (CollectionUtil.isNotEmpty(source.getRecords())) {
                List<T> list = source.getRecords();
                List<V> ds = list.stream().map(m -> {
                    D dObj = null;
                    try {
                        dObj = dClass.newInstance();
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    return convert.apply(m, dObj);
                }).collect(Collectors.toList());
                dest.setRecords(ds);
            }
            return dest;
        }
        return null;
    }

    /**
     * 分页转换,简化版
     *
     * @return
     */
    public static <D, T> Page<D> toDtoPage3(Page<T> source, Class<D> dClass) {
        if (source != null) {
            Page<D> dest = new Page<>(source.getCurrent(), source.getSize(), source.getTotal());
            if (CollectionUtil.isNotEmpty(source.getRecords())) {
                List<T> list = source.getRecords();
                List<D> ds = list.stream().map(m -> {
                    D d = null;
                    try {
                        d = dClass.newInstance();
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    BeanUtils.copyProperties(m, d);
                    return d;
                }).collect(Collectors.toList());
                dest.setRecords(ds);
            }
            return dest;
        }
        return null;
    }

    /**
     * 列表转换
     *
     * @return
     */
    public static <D, T> List<D> toDtoList(List<T> source, Class<D> dClass) {
        if (CollectionUtil.isNotEmpty(source)) {
            List<D> ds = source.stream().map(m -> {
                D d = null;
                try {
                    d = dClass.newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                BeanUtils.copyProperties(m, d);
                return d;
            }).collect(Collectors.toList());
            return ds;
        }
        return null;
    }
}
